package com.itheima.config;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;

/**
 * Description:
 *
 * @author codermao
 * @date 2023/11/3 13:06
 */

/**
 * 替代了：MyBatisFactory_quit + mybatis核心配置文件
 */
public class MyBatisConfig {
    /**
        SqlSessionFactoryBean (mybatis的spring整合包提供的类)
        1. 底层会提供 SqlSessionFactory
        2. 当开发者需要注入dao层bean的,此SqlSessionFactory会自动提供, 省略过程变量
            1). 还需要设置dao层的扫描

     SqlSessionFactoryBuilder --> SqlSessionFactory --> SqlSession --> AccountDao
        扫描到dao层的接口之后会自动帮我们创建dao层的bean --》 AccountDao
     */
    @Bean
    public SqlSessionFactoryBean getSqlSessionFactoryBean(DataSource ds){
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        //设置pojo的包扫描,别名包扫描
        factoryBean.setTypeAliasesPackage("com.itheima.domain");
        //设置连接池
        factoryBean.setDataSource(ds);

        return factoryBean;
    }
    //定义bean，返回MapperScannerConfigurer对象(mybatis的spring整合包提供的类)
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
        //设置dao层的接口扫描，接口包扫描
        msc.setBasePackage("com.itheima.dao");
        return msc;
    }
}
